<template>
  <!--  TODO 从vue-element-admin抄过来，无法适配uni-app -->
  <!--  有一个良性bug，seg-upload装载的时候，会计算一次showRightPanel，导致手机端刚开始就会弹出来 -->
  <div ref="rightPanel" :class="{show: segPanelStatus.showRightPanel}" >
    <view class="rightPanel-background" @click="changeRightMenuShowState(segPanelStatus.showRightPanel)"/>
    <view class="rightPanel">
<!--      <view class="handle-button" :style="{'top':buttonTop+'px','background-color':theme}" @click="show=!show">-->
<!--        <uni-icons type="settings-filled" size="30"></uni-icons>-->
<!--      </view>-->
      <view>
        <slot />
      </view>
    </view>
  </div>
</template>

<script>
import {mapActions, mapState} from 'vuex'
import UniIcons from "../uni-icons/uni-icons";
export default {
  name: 'segRightMenu',
  components: {UniIcons},
  props: {
    clickNotClose: {
      default: false,
      type: Boolean
    },
    buttonTop: {
      default: 200,
      type: Number
    }
  },
  data() {
    return {
      show: false
    }
  },
  computed: {
    theme() {
      return '#00CD66'
    },
    ...mapState('app', ['segPanelStatus'])
  },
  mounted() {
    this.insertToBody()
  },
  beforeDestroy() {
    const elx = this.$refs.rightPanel
    elx.remove()
  },
  methods: {
    ...mapActions({
      setRightMenu: 'app/setSegPanelState'
    }),
    changeRightMenuShowState(res){
      let segPanelStatus = {
        showRightPanel: !res
      }
      this.setRightMenu(segPanelStatus)
    },
    closeSidebar(evt) {
      const parent = evt.target.closest('.rightPanel')
      if (!parent) {
        this.show = false
        window.removeEventListener('click', this.closeSidebar)
      }
    },
    insertToBody() {
      // debugger
      const elx = this.$refs.rightPanel
      const body = document.querySelector("uni-page-body")
      body.insertBefore(elx, body.firstChild)
    }
  }
}
</script>

<style lang="scss" scoped>
.showRightPanel {
  overflow: hidden;
  position: relative;
  width: calc(100% - 15px);
}
.rightPanel-background {
  position: fixed;
  top: 0;
  left: 0;
  opacity: 0;
  transition: opacity .3s cubic-bezier(.7, .3, .1, 1);
  background: rgba(0, 0, 0, .2);
  z-index: -1;
}

.rightPanel {
  width: 100%;
  max-width: 260px;
  height: 100vh;
  position: fixed;
  top: 0;
  right: 0;
  box-shadow: 0px 0px 15px 0px rgba(0, 0, 0, .05);
  transition: all .25s cubic-bezier(.7, .3, .1, 1);
  transform: translate(100%);
  background: #fff;
  z-index: 40000;
}

.show {
  transition: all .3s cubic-bezier(.7, .3, .1, 1);

  .rightPanel-background {
    z-index: 20000;
    opacity: 1;
    width: 100%;
    height: 100%;
  }

  .rightPanel {
    transform: translate(0);
  }
}

.handle-button {
  width: 48px;
  height: 48px;
  position: absolute;
  left: -48px;
  text-align: center;
  font-size: 24px;
  border-radius: 6px 0 0 6px !important;
  z-index: 0;
  pointer-events: auto;
  cursor: pointer;
  color: #fff;
  line-height: 48px;
  i {
    font-size: 24px;
    line-height: 48px;
  }
}
</style>
